package com.angcyo.selenium.auto.action

import com.angcyo.javafx.base.ex.syncBack
import com.angcyo.javafx.ui.alertConfirm
import com.angcyo.library.ex.count
import com.angcyo.selenium.auto.BaseControl
import com.angcyo.selenium.auto.parse.HandleResult
import com.angcyo.selenium.bean.ActionBean
import org.openqa.selenium.WebElement

/**
 * Email:angcyo@126.com
 * @author angcyo
 * @date 2021/01/18
 */
class ConfirmAction : BaseCodeAction() {

    override fun interceptAction(control: BaseControl, action: String?): Boolean {
        return action?.startsWith(Action.ACTION_CONFIRM) == true
    }

    override fun runAction(
        control: BaseControl,
        element: WebElement,
        actionBean: ActionBean,
        action: String
    ): HandleResult {
        val result = HandleResult()
        result.elementList = listOf(element)

        //解析参数
        var title = "提示"
        var tip = "需要确认后, 才可以继续操作."

        if (action.count(' ') > 0) {
            //如果包含了空格, 则使用最新的正则参数解析方法.
            title = action.arg("title")?.run { control._autoParse.parseText(control, this).firstOrNull() } ?: title
            tip = action.arg("tip")?.run { control._autoParse.parseText(control, this).firstOrNull() } ?: tip
        }

        val value = syncBack {
            alertConfirm(tip) {
                this.title = title
            }
        }
        result.success = value == true

        control.logAction?.invoke("手动确认:[${title}][$tip]:${result.success}")
        return result
    }
}